package com.xm.xmapp.customview;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.xm.xmapp.R;
import com.xm.xmapp.myinterface.MyDialogButton;

public class NameDialog extends Dialog {

	private Context context;
	private TextView left, right;
	private MyDialogButton in;
	private EditText content;
	private String title_str;
	private int inputType;

	public NameDialog(Context context, String title, MyDialogButton in, int inputType) {
		super(context, R.style.Dialog);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.title_str = title;
		this.in = in;
		this.inputType = inputType;
	}

	@SuppressLint("InflateParams")
	public void init() {
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.dialog_sexandname, null);
		TextView title = (TextView) view.findViewById(R.id.title);
		content = (EditText) view.findViewById(R.id.content);
		content.setInputType(inputType);
		left = (TextView) view.findViewById(R.id.left);
		right = (TextView) view.findViewById(R.id.right);
		title.setText(title_str == null ? "" : title_str);

		left.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				dismiss();
			}
		});

		right.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (in != null) {
					dismiss();
					in.setTitle(1, content.getText().toString());
				}

			}
		});

		if ((InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL) == inputType) {
			content.addTextChangedListener(new TextWatcher() {

				@Override
				public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
					if (arg0.toString().length() == 1 && arg0.toString().equals("0")) {
						content.setText("");
					}

				}

				@Override
				public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

				}

				@Override
				public void afterTextChanged(Editable arg0) {

				}
			});

			content.setFilters(new InputFilter[] { new InputFilter() {
				@Override
				public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
					if (source.equals(".") && dest.toString().length() == 0) {
						return "0.";
					}
					if (dest.toString().contains(".")) {
						int index = dest.toString().indexOf(".");
						int mlength = dest.toString().substring(index).length();
						if (mlength == 3) {
							return "";
						}
					}
					return null;
				}
			} });
		}
		setContentView(view);

		Window dialogWindow = getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
		lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
		dialogWindow.setAttributes(lp);
	}

	public void setContent(String str) {
		if (str == null || str.equals(""))
			return;
		content.setText(str);

	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
		/** 3.自动弹出软键盘 **/
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				content.setFocusable(true);
				content.setFocusableInTouchMode(true);
				content.requestFocus();
				InputMethodManager inputManager = (InputMethodManager) content.getContext().getSystemService(
						Context.INPUT_METHOD_SERVICE);
				inputManager.showSoftInput(content, 0);
			}
		}, 200);		

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();
	}
}
